// SmartPointer.h
#pragma once
#include "Object.h"

namespace ANvDbg
{
    template<class T>
    class SmartPoint : public Object
    {
    protected:
        SmartPoint() {}

    public:
        SmartPoint(T* ptr) : m_ptr(ptr)
        {
            if (m_ptr != nullptr)
            {
                ++m_ptr->m_reference_count;
            }
        }

        ~SmartPoint()
        {
            if (m_ptr != nullptr)
            {
                --m_ptr->m_reference_count;
            }
        }

    public:
        T* operator->() { return m_ptr; }

        void operator=(T* ptr)
        {
            if (ptr != nullptr)
            {
                --ptr->m_reference_count;
            }

            m_ptr = ptr;
            if (m_ptr != nullptr)
            {
                ++m_ptr->m_reference_count;
            }
        }

    public:
        T* m_ptr;
    };
}
